function onLoad() {

}

function showSpinner(){
	$("#spinner").show();
}

function hideSpinner(){
	$("#spinner").hide();
}

function escondeData(names) {
	for (i = 0; i < names.length; i++) {
		var day = document.getElementById(names[i]+"_day");
		day.style.display = 'none';
		day.selectedIndex = 0;
		
		var month = document.getElementById(names[i]+"_month");
		month.style.display = 'none';
		month.selectedIndex = 0;
		
		var year = document.getElementById(names[i]+"_year");
		year.style.display = 'none';
		year.selectedIndex = 0;
	}
}


function cliquePresenca(checkBox){
	showSpinner();
	contabilizaPresenca(checkBox.checked, checkBox.getAttribute("faltas"));
	var args = "presencaId="+checkBox.name;
	args += "&presente=" + (checkBox.checked == true ? "S" : "N");
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/diario/gravaPresenca",
		  dataType : "script",
		  data: args,
		  success: function(data) {
		    
		  },
		  
		});
	hideSpinner();
}


function contabilizaPresenca(checked, idEditFalta){
	var faltas = $("#"+idEditFalta).val();
	faltas = parseInt(faltas);
	faltas = checked ? faltas-1 : faltas+1;
	$("#"+idEditFalta).val(faltas);
}

function getUrl( params){
	return (""+window.location.protocol +"//"+ window.location.host + window.location.pathname + "?"+ params);

}



function gerarAulas(){
	showSpinner();
	$("#divMensagem").hide();
	var dataIni = $("#dataInicial_day").val()+"/"+$("#dataInicial_month").val()+"/"+$("#dataInicial_year").val();
	var dataFin = $("#dataFinal_day").val()+"/"+$("#dataFinal_month").val()+"/"+$("#dataFinal_year").val();
	var turmaId = $("#turma").val();
	
	
	var data = "dataIni="+encodeURIComponent(dataIni);
	data += "&dataFin="+encodeURIComponent(dataFin);
	data += "&turmaId="+(turmaId == "null" ? -1 : turmaId);
	
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/geracaoAula/gerar",
		  dataType : "text",
		  data: data,
		  success: function(data) {
		    eval(data);
		    var html = "<ul>";
		    for(i = 0; i < gerado.length; i++){
		    	html += "<li>"+gerado[i].msg+"</li>";
		    }
		    html += "</ul>";
		    
		    $("#mensagem").html(html);
		    $("#divMensagem").show();
		  },
		  
		});
	hideSpinner();
}


function carregaEmails(){
	showSpinner();
	var data = "";
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/pessoa/listaEmail",
		  dataType : "text",
		  data: data,
		  success: function(data) {
		    eval(data);
		    var html = "";
		    for(i = 0; i < emails.length; i++){
		    	html += emails[i].email+"; ";
		    }
		    
		    $("#conteudoMsg").html(html);
		  },
		  
		});
	
	$("#emailsWindow").modal({opacity:80, overlayCss: {backgroundColor:"#000"}});
	hideSpinner();
}


function buscaInfPessoa(pessoaId){
	showSpinner();
	var data = "id="+pessoaId;
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/pessoa/infMensalidade",
		  dataType : "text",
		  data: data,
		  success: function(data) {
			eval(data);
			
			$("#valor").val(pessoa.mensalidade);
			//$("#dataVct_day").val(pessoa.diaVct);
			
			if(pessoa.diaVct != ""){
				var encontrou = false;
				var comboDay = document.getElementById("dataVct_day");
				var dayVct = pessoa.diaVct;
				while(!encontrou){
					comboDay.selectedIndex = dayVct-1;
					if(comboDay.options[comboDay.selectedIndex].value == dayVct){
						encontrou = true;
					}else{
						dayVct--;
						if(dayVct < 1 || dayVct > 31){
							encontrou = true;
						}
					}
				}
			}
			$("#numero").val(pessoa.numero);
		    
		  }
		});
	
		if($("#situacao").val() == "ABERTO"){
			document.getElementById("dataPag_day").selectedIndex = 0;
			document.getElementById("dataPag_month").selectedIndex = 0;
			document.getElementById("dataPag_year").selectedIndex = 0;
		}
		hideSpinner();
}


function gerarMensalidade(){
	showSpinner();
	$("#divMensagem").hide();
	var dataIni = $("#dataInicial_day").val()+"/"+$("#dataInicial_month").val()+"/"+$("#dataInicial_year").val();
	var dataFin = $("#dataFinal_day").val()+"/"+$("#dataFinal_month").val()+"/"+$("#dataFinal_year").val();
	var turmaId = $("#turma").val();
	var pessoaId = $("#pessoa").val();
	
	
	var data = "dataIni="+encodeURIComponent(dataIni);
	data += "&dataFin="+encodeURIComponent(dataFin);
	data += "&turmaId="+(turmaId == "null" ? -1 : turmaId);
	data += "&pessoaId="+(pessoaId == "null" ? -1 : pessoaId);
	
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/geracaoMensalidade/gerar",
		  dataType : "text",
		  data: data,
		  async : false,
		  success: function(data) {
		    eval(data);
		    var html = "<ul>";
		    for(i = 0; i < gerado.length; i++){
		    	html += "<li>"+gerado[i].msg+"</li>";
		    }
		    html += "</ul>";
		    
		    $("#mensagem").html(html);
		    $("#divMensagem").show();
		  },
		  
		});
	hideSpinner();
}


function pagarMensalidade(mensalidadeId){
	showSpinner();
	if(!confirm("Deseja pagar a mensalidade?")){
		hideSpinner();
		return;
	}
	
	var data = "mensalidadeId="+mensalidadeId;
	
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/mensalidade/pagar",
		  dataType : "script",
		  async : false,
		  data: data,
		  scriptCharsetString  : "ISO-8859-1",
		  contentType : "text/html;charset=ISO-8859-1",
		  success: function(data) {
		  }
		});
	hideSpinner();
	
}

function gerarAulaMesAtual(turmaId){
	showSpinner();
	var data = "turmaId="+turmaId;
	
	$.ajax({
		  url: ""+window.location.protocol +"//"+ window.location.host+"/Pilates/diario/gerarAulaMesAtualAjax",
		  dataType : "script",
		  async : false,
		  data: data,
		  scriptCharsetString  : "ISO-8859-1",
		  contentType : "text/html;charset=ISO-8859-1",
		  success: function(data) {
		  }
		});
	hideSpinner();
}

function visualizarFinanceiro(bookMark){
	showSpinner();
	var dataIni = $("#dataInicial_day").val()+"/"+$("#dataInicial_month").val()+"/"+$("#dataInicial_year").val();
	var dataFin = $("#dataFinal_day").val()+"/"+$("#dataFinal_month").val()+"/"+$("#dataFinal_year").val();
	
	$("#dataIni").val(dataIni);
	$("#dataFin").val(dataFin);
	var form = document.forms["frFinanceiro"];
	if(bookMark != "" && bookMark != null){
		form.setAttribute("action", form.getAttribute("action")+"#"+bookMark);
	}
	form.submit();
	hideSpinner();
}

function pagarMensalidadeReload(mensalidadeId, pessoaId){
	pagarMensalidade(mensalidadeId);
	visualizarFinanceiro(pessoaId);
}

function pagarMensalidadePessoa(mensalidadeId){
	pagarMensalidade(mensalidadeId);
	document.location = ""+window.location.protocol +"//"+ window.location.host+"/Pilates/pessoa/edit/?id="+$("#id").val()+"&aba=fin";
	
}